package com.surveypark.struts2.interceptor;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.surveypark.service.RightService;
import com.surveypark.util.ValidateUtil;

public class CatchUrlInterceptor implements Interceptor{

	private static final long serialVersionUID = 4776970164582841152L;

	@Override
	public void destroy() {
		
	}

	@Override
	public void init() {
		
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionProxy proxy = invocation.getProxy();
		String ns = proxy.getNamespace();
		String actionName = proxy.getActionName();
		
		if(!ValidateUtil.isValid(ns) || "/".equals(ns)){
			ns = "";
		}
		String url = ns + "/" + actionName;
		
		ServletContext sc = ServletActionContext.getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		RightService rs = (RightService)ac.getBean("rightService");
		rs.appendRightByURL(url);
		return invocation.invoke();
	}

}
